package ywpt.web.action.config;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletException;

import org.ecside.table.limit.Limit;
import org.ecside.util.RequestUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

import ywpt.components.acegi.domain.Operation;
import ywpt.core.Constants;
import ywpt.core.SystemMessage;
import ywpt.core.exception.UniqueConstraintException;
import ywpt.core.web.BaseAction;
import ywpt.model.AssetsInfo;
import ywpt.service.config.AssetsInfoService;
import ywpt.web.common.OperationConstants;
import ywpt.web.common.PublicFunction;

public class AssetsInfoAction extends BaseAction implements ModelDriven<AssetsInfo> , Preparable{

	/**
	 * 公司资产Action
	 */
	private static final long serialVersionUID = 8410281087015060109L;
	private static final int DEFAULT_PAGE_SIZE = 100;
	private boolean[] popedom=new boolean[Constants.POPEDOM_LEN];
	private Long id;
	private AssetsInfo assetsInfo;
	private List<AssetsInfo> assetsInfoList = new ArrayList<AssetsInfo>();
	private AssetsInfoService assetsInfoService;
	
	private Long typeId;
	private Long departId;
	private Long peopleId;
	private String peopleName;
	

	public String getPeopleName() {
		return peopleName;
	}

	public void setPeopleName(String peopleName) {
		this.peopleName = peopleName;
	}

	public boolean[] getPopedom() {
		return popedom;
	}

	public void setPopedom(boolean[] popedom) {
		this.popedom = popedom;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public AssetsInfo getAssetsInfo() {
		return assetsInfo;
	}

	public void setAssetsInfo(AssetsInfo assetsInfo) {
		this.assetsInfo = assetsInfo;
	}

	public List<AssetsInfo> getAssetsInfoList() {
		return assetsInfoList;
	}

	public void setAssetsInfoList(List<AssetsInfo> assetsInfoList) {
		this.assetsInfoList = assetsInfoList;
	}

	public AssetsInfoService getAssetsInfoService() {
		return assetsInfoService;
	}

	public void setAssetsInfoService(AssetsInfoService assetsInfoService) {
		this.assetsInfoService = assetsInfoService;
	}

	public Long getTypeId() {
		return typeId;
	}

	public void setTypeId(Long typeId) {
		this.typeId = typeId;
	}

	public Long getDepartId() {
		return departId;
	}

	public void setDepartId(Long departId) {
		this.departId = departId;
	}

	public Long getPeopleId() {
		return peopleId;
	}

	public void setPeopleId(Long peopleId) {
		this.peopleId = peopleId;
	}

	/*----------------------------------------------------------------------------------------*/
	@Override
	public AssetsInfo getModel() {
		return this.assetsInfo;
	}

	@Override
	public void prepare() throws Exception {
		if(id!=null){
			this.assetsInfo = this.assetsInfoService.getAssetsInfoById(id);
		}else{
			this.assetsInfo = new AssetsInfo();
		}
		HashMap<String,Operation> oper=this.getOperMapByUser(this.getCurrentUser());
		PublicFunction.getOperationPopedom(oper,popedom,OperationConstants.CONFIG_ASSETS);
	}
	protected void showInfo() {
		if(this.assetsInfo.getAssetsType()!=null)
			this.typeId = this.assetsInfo.getAssetsType().getId();
		if(this.assetsInfo.getManageOrg()!=null)
			this.departId = this.assetsInfo.getManageOrg().getId();
		if(this.assetsInfo.getManageUser()!=null){
			this.peopleId = this.assetsInfo.getManageUser().getId();
			this.peopleName =  this.assetsInfo.getManageUser().getPeopleName();
		}
			
	}
	/*========================================================================================*/
	public String list() throws Exception {
		this.assetsInfoList.clear();
		DetachedCriteria query = DetachedCriteria.forClass(AssetsInfo.class);
		query.add(Restrictions.eq("deleteFlag", false));
		int totalRows=RequestUtils.getTotalRowsFromRequest(getRequest());
		if(totalRows<0)
		{
			totalRows= assetsInfoService.getAssetsInfoCountByCriteria(query);
		}
		int[] rowStartEnd=RequestUtils.getRowStartEnd(getRequest(),totalRows,
				DEFAULT_PAGE_SIZE,0);
		Limit limit=RequestUtils.getLimit(getRequest(),"ec",totalRows,
				DEFAULT_PAGE_SIZE);
		int pageSize=limit.getCurrentRowsDisplayed();
		query.addOrder(Order.asc("id"));
		query.setProjection(null);
		assetsInfoList.addAll(assetsInfoService.getAssetsInfos(query, pageSize, rowStartEnd[0]));		
		return SUCCESS;
	}
	
	public String input(){
		String codeStr = this.assetsInfoService.creatAssetsNo();
		this.assetsInfo.setAssetsNo(codeStr);
		return INPUT;
	}	
	
	public String edit(){
		this.assetsInfo = this.assetsInfoService.getAssetsInfoById(id);
		showInfo();
		return INPUT;
	}	
	
	public String save() throws ServletException
	{
		try
		{
			if(id==null)
			{   assetsInfo.setCreater(this.getCurrentUser());
				assetsInfo.setCreateDate(new Date());
				assetsInfoService.saveAssetsInfo(assetsInfo,typeId,departId,peopleId);
				this.addActionMessage(SystemMessage.SAVE_SUCCESS);
			}
			else
			{
				if(!assetsInfo.getCheckFlag()){
					assetsInfo.setModifyer(this.getCurrentUser());
					assetsInfo.setModifyDate(new Date());
					assetsInfoService.updateAssetsInfo(assetsInfo,typeId,departId,peopleId);
					this.addActionMessage(SystemMessage.UPDATE_SUCCESS);
				}else{
					this.addActionMessage(SystemMessage.AUDITING_SAVE);
				}
					
			}			
		}	
		catch (UniqueConstraintException e) 
		{
			this.addActionError(SystemMessage.SAVE_FAIL+e.getMessage());
			return SUCCESS;
		}
		showInfo();
		return SUCCESS;
	}

	public String check()
	{
		if(id==null){
			this.addActionMessage(SystemMessage.NO_OBJECT);
			
		}else{
			assetsInfo=assetsInfoService.getAssetsInfoById(id);
		if(!assetsInfo.getCheckFlag())
		{
			assetsInfo.setChecker(this.getCurrentUser());
			assetsInfo.setCheckFlag(true);
			assetsInfoService.updateAssetsInfo(assetsInfo,null,null,null);
			this.addActionMessage(SystemMessage.AUDITING_SUCCESS);
		}
		else
			this.addActionMessage(SystemMessage.AUDITING_REPEAT);
		
		}
		showInfo();
		return SUCCESS;
	}
	
	public String discheck()
	{
		if(id==null){
			this.addActionMessage(SystemMessage.NO_OBJECT);
			
		}else{
			assetsInfo=assetsInfoService.getAssetsInfoById(id);
		if(assetsInfo.getCheckFlag())
		{
			assetsInfo.setChecker(null);
			assetsInfo.setCheckFlag(false);
			assetsInfoService.updateAssetsInfo(assetsInfo,null,null,null);
			this.addActionMessage(SystemMessage.DISAUDITING_SUCCESS);
		}
		else
			this.addActionMessage(SystemMessage.DISAUDITING_REPEAT);
		
		}
		showInfo();
		return SUCCESS;
	}
	
	public String delete()
	{
		if(id!=null)
		{
			try
			{	
				if(!assetsInfo.getCheckFlag()){
					assetsInfoService.removeAssetsInfo(id);
				}else{
					this.addActionMessage(SystemMessage.DELETE_AUDITING_FAIL);
					showInfo();
					return INPUT;
				}
				
			}
			catch(UniqueConstraintException e)
			{
				this.addActionMessage(e.getMessage());
				showInfo();
				return INPUT;
			}
		}
		else
		{
			this.addActionMessage(SystemMessage.NO_OBJECT);
			showInfo();
			return INPUT;
		}
		return SUCCESS;
	}
	
	
	
	
	
	
	
	
}
